import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { IAccount } from '/@/interfaces'

type AccountState = IAccount

const initialState: AccountState = {
  username: ''
}

export const accountSlice = createSlice({
  name: 'account',
  initialState,
  reducers: {
    persist(state, action: PayloadAction<IAccount>): AccountState {
      const { username, avatar } = action.payload
      return {
        ...state,
        username,
        avatar
      }
    },
    wipe(state): AccountState {
      return {
        ...state,
        username: '',
        avatar: undefined
      }
    }
  }
})

export const { persist, wipe } = accountSlice.actions
export default accountSlice.reducer
